package com.hongxun.design.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.bean.BitTypeTabItemBean;
import com.hongxun.design.bean.CodeSetVersionBean;
import com.hongxun.design.bean.CodelibraryConditionBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ShardPreferencesTool;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.pickerview.OptionsPopupWindow;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringCodeTableUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/24.
 */
public class CodelibraryConditionAdapter extends BaseAdapter {
    Context context;
    List<CodelibraryConditionBean> itemBeans;
    LayoutInflater inflater;

    public CodelibraryConditionAdapter(Context context, List<CodelibraryConditionBean> itemBeans) {
        this.context = context;
        this.itemBeans = itemBeans;
        inflater = LayoutInflater.from(context);
    }

    public List<CodelibraryConditionBean> getData() {
        return itemBeans;
    }

    public void setData(Object value, int position) {
        CodelibraryConditionBean bean = itemBeans.get(position);
        bean.setValuecode(((CodeSetVersionBean) value).getCodeset_version_id());
        bean.setValue(((CodeSetVersionBean) value).getCodeset_version());
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        return itemBeans.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final CodelibraryConditionBean bean = (CodelibraryConditionBean) getItem(i);
        int viewtype = getItemViewType(i);
        Holledler holledler = null;
        ButtonHolledler buttonHolledler = null;

        if (viewtype == Constant.onlyOut) {
            view = inflater.inflate(R.layout.listview_item_search, null);
            buttonHolledler = new ButtonHolledler(view);
            view.setTag(buttonHolledler);
        } else {
            view = inflater.inflate(R.layout.listview_item_clickanded, null);
            holledler = new Holledler(view);
            view.setTag(holledler);
        }
        try {
            if (viewtype == Constant.onlyWrite) {
                holledler.name.setText(bean.getName());
                holledler.edtext.setVisibility(View.VISIBLE);
                holledler.value.setVisibility(View.GONE);
                holledler.right.setVisibility(View.GONE);
                String value = bean.getValuecode();
                if (value.length() == 0) {
                    value = "未填写";
                    holledler.edtext.setHint(value);
                } else {
                    holledler.edtext.setText(value);
                }
                holledler.edtext.addTextChangedListener(new MTextWatcher(bean));
                holledler.item.setOnClickListener(null);
            } else if (viewtype == Constant.onlyActivity) {
                holledler.name.setText(bean.getName());
                holledler.edtext.setVisibility(View.GONE);
                holledler.value.setVisibility(View.VISIBLE);
                holledler.right.setVisibility(View.VISIBLE);
                String value = bean.getValue();
                if (value.length() == 0) {
                    value = "未选择";
                }
                holledler.value.setText(value);
                holledler.item.setOnClickListener(new MClickListener(i, bean) {
                    @Override
                    public void onClick(View view, final int position, Object msg) {
                        final CodelibraryConditionBean b = (CodelibraryConditionBean) msg;
                        if (b.getNamecode().equals(MString.codeset_version_id)) {
                            ArrayList<String> list1 = new ArrayList<>();
                            ArrayList<ArrayList<String>> list2 = new ArrayList<>();
                            //典设版本
                            final List<SystemTypeBean> beans = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.codesetType);
                            final List<List<CodeSetVersionBean>> beenall = new ArrayList<>();
                            int index = 0;
                            for (SystemTypeBean bean : beans) {
                                String typename = bean.getTypename();
                                if (typename.equalsIgnoreCase("项目")) continue;
                                if (typename.equalsIgnoreCase("用户")) typename = "收藏";
                                list1.add(typename);
                                String typecode = bean.getTypecode();
                                //对应典设版本的典设
                                String getshare = ShardPreferencesTool.getshare(context, MString.codeset_type + typecode, "");
                                List<CodeSetVersionBean> been = FastJsonUtil.JsonToData_CodeSetVersion(getshare);
                                ArrayList<String> list3 = new ArrayList<>();
                                ArrayList<CodeSetVersionBean> beenall1 = new ArrayList<>();
                                for (CodeSetVersionBean versionBean : been) {
                                    String type = versionBean.getCodeset_version();
                                    list3.add(type);
                                    beenall1.add(versionBean);
                                }
                                list2.add(index, list3);
                                beenall.add(index, beenall1);
                                index++;
                            }
                            final OptionsPopupWindow pwOptions = new OptionsPopupWindow(context);
                            pwOptions.setPicker(list1, list2, true);
                            pwOptions.setItemLength(10);
//                            final ArrayList<String> startTime = pwOptions.getpicker1();
                            final ArrayList<ArrayList<String>> endTime = pwOptions.getpicker2();
                            pwOptions.setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
                                @Override
                                public void onOptionsSelect(int options1, int option2, int options3) {
//                                    String first = startTime.get(options1);
                                    if (endTime.get(options1).isEmpty()) {
                                        pwOptions.dismiss();
                                        ToastShow.showShort(context, "暂无平台典设");
                                        return;
                                    }
                                    String end = endTime.get(options1).get(option2);
//                                    b.setValue(end + "—" + first);
                                    b.setValue(end);
                                    b.setValuecode(beenall.get(options1).get(option2).getCodeset_version_id());
                                    notifyDataSetChanged();
                                }
                            });
                            pwOptions.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                        } else {
                            ArrayList<String> list1 = new ArrayList<>();
                            final List<BitTypeTabItemBean> bit = DataUtil.getBitTypeData(1);
                            BitTypeTabItemBean itemBean = new BitTypeTabItemBean();
                            itemBean.setName("全部");
                            itemBean.setId("");
                            bit.add(itemBean);
                            final OptionsPopupWindow pwOptions = new OptionsPopupWindow(context);
                            for (BitTypeTabItemBean bean : bit) {
                                list1.add(bean.getName());
                            }
                            pwOptions.setPicker(list1);
                            pwOptions.setItemLength(10);
                            final ArrayList<String> startTime = pwOptions.getpicker1();
                            pwOptions.setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
                                @Override
                                public void onOptionsSelect(int options1, int option2, int options3) {
                                    if (startTime.isEmpty()) {
                                        pwOptions.dismiss();
                                        ToastShow.showShort(context, "暂无平台典设");
                                        return;
                                    }
                                    String first = startTime.get(options1);
                                    b.setValue(first);
                                    b.setValuecode(bit.get(options1).getId());
                                    notifyDataSetChanged();
                                }
                            });
                            pwOptions.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                        }
                    }
                });
            } else if (viewtype == Constant.onlyOut) {
                buttonHolledler.search.setOnClickListener(new MClickListener(i, bean) {
                    @Override
                    public void onClick(View view, int position, Object msg) {
                        ((BaseActivity) context).RefreshData();
                    }
                });
            }
        } catch (Exception e) {
        }
        return view;
    }

    class Holledler {
        RelativeLayout item;
        TextView name, right, value;
        EditText edtext;

        Holledler(View v) {
            item = (RelativeLayout) v.findViewById(R.id.clickanded_item);
            name = (TextView) v.findViewById(R.id.clickanded_name);
            edtext = (EditText) v.findViewById(R.id.clickanded_edtext);
            right = (TextView) v.findViewById(R.id.clickanded_right);
            value = (TextView) v.findViewById(R.id.clickanded_value);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
        }
    }

    class ButtonHolledler {
        TextView search;

        ButtonHolledler(View v) {
            search = (TextView) v.findViewById(R.id.clickanded_search);
            SetTypeface.set(search, IconSourse.SEARCH);
            search.append(" 搜索");
        }
    }


    class MTextWatcher implements TextWatcher {
        CodelibraryConditionBean bean;

        MTextWatcher(CodelibraryConditionBean bean) {
            this.bean = bean;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (!bean.getValuecode().equals(editable.toString())) {
                bean.setValuecode(editable.toString());
            }
        }
    }
}
